﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class GameManager : MonoBehaviour
{
    // Start is called before the first frame update
    [SerializeField]
    private Player player;
    private npc currentTRGT;
    private Camera mainCam;
    void Start()
    {
        mainCam = Camera.main;
    }

    // Update is called once per frame
    void Update()
    {
        clickTrgt();
    }
    private void clickTrgt()
    {
        if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
        {
            Vector3 MousePos = Input.mousePosition;
            Vector3 position = mainCam.ScreenToWorldPoint(MousePos);
            RaycastHit2D hit = Physics2D.Raycast(position, Vector2.zero, Mathf.Infinity, LayerMask.GetMask("clickable"));

            if (hit.collider != null)
            {
                if (currentTRGT != null)
                {
                    currentTRGT.Deselect();

                }
                if (hit.collider.GetComponent<npc>() != null)
                {
                    currentTRGT = hit.collider.GetComponent<npc>();

                }
                else if (hit.collider.GetComponentInParent<npc>() != null)
                {
                    currentTRGT = hit.collider.GetComponentInParent<npc>();
                }
                if (currentTRGT)
                {
                    player.MyTrgt = currentTRGT.Select();
                }
            }
            else
            {
                if (currentTRGT != null)
                {
                    currentTRGT.Deselect();

                }
                player.MyTrgt = null;
                currentTRGT = null;


            }
        }
        else if (Input.GetMouseButtonDown(1) && !EventSystem.current.IsPointerOverGameObject())
        {
            
            Vector3 MousePos = Input.mousePosition;
            Vector3 position = mainCam.ScreenToWorldPoint(MousePos);
            RaycastHit2D hit = Physics2D.Raycast(position, Vector2.zero, Mathf.Infinity, LayerMask.GetMask("clickable"));
            IInteractable tempInteraction = hit.collider.gameObject.GetComponent<IInteractable>();
            if (
                    hit.collider != null 
                    && 
                    (
                        hit.collider.tag == "Enemy" 
                        || 
                        hit.collider.tag == "interacteble"
                    ) 
                    &&
                    player.MyInteractables.Contains(tempInteraction)
               )
            {

                tempInteraction.Interact();
            }
            //if (hit.collider != null)
            //{

            //    if (hit.collider.tag == "Enemy" ||
            //       hit.collider.tag == "interacteble")
            //    {

            //        player.CheckInteraction();


            //        if (player.MyInteractables.Contains(hit.collider.GetComponent<IInteractable>()))
            //        {
            //            player.MyInteractable = hit.collider.GetComponent<IInteractable>();


            //            player.Interact();
            //        }
            //    }

            //}
        }
    }
}
